package com.example.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Remember;
import com.example.springboot.entity.Setting;
import com.example.springboot.mapper.RememberMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/remember")
public class RememberController {

    @Resource
    RememberMapper rememberMapper;

    @PostMapping("/getNumber")
    public Result<?> getRememberNumber(){
        QueryWrapper<Remember> wrapper = Wrappers.query();
        wrapper.last("limit 30");
        List<Remember> list = rememberMapper.selectList(wrapper);
        List<Integer> numberList = new ArrayList<>();
        list.forEach(item->numberList.add(item.getNumber()));
        return Result.success(numberList);
    }
    @PostMapping("/getDate")
    public Result<?> getRememberDate(){
        SimpleDateFormat f = new SimpleDateFormat("dd");
        QueryWrapper<Remember> wrapper = Wrappers.query();
        wrapper.last("limit 30");
        List<Remember> list = rememberMapper.selectList(wrapper);
        List<String> dateList = new ArrayList<>();
        list.forEach(item->dateList.add(f.format(item.getRdate())));
        return Result.success(dateList);
    }
    @PostMapping("/insertNumber")
    public Result<?> insertNumber(@RequestBody Setting setting) {
        Remember remember = new Remember();
        remember.setNumber(setting.getTodayDone());
        remember.setRdate(new Date(System.currentTimeMillis()));
        rememberMapper.insert(remember);
        return Result.success();
    }
}
